<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/12
 * Time: 14:05
 */
use Org\Util\Rbac;

//获取当前登录用户ID: 如果尚未登陆，则返回NULL
function getUserId(){
    return session(C('USER_AUTH_KEY'));
}

function getUser(){
    if(getUserId()){
        return D('Crm/User')->field('password',true)->relation(array('role'))->find(getUserId());
    }
}

function getRoles(){
    $user = getUser();
    if($user){
        $roles = $user['role'];

    }
    return $roles;
}

function getAccessList(){
    return Rbac::getAccessList(getUserId());
}

function checkAccess(){
    $accessList = getAccessList();
    return $accessList[strtoupper(MODULE_NAME)][strtoupper(CONTROLLER_NAME)][strtoupper(ACTION_NAME)];
}

function isCs(){
    $roles = getRoles();
    foreach($roles as $k=>$v){
        if($v['id'] == 3){
            return true;
        }
    }
    return false;
}

function generateCustomerNo($date){
    return date('YmdHis',strtotime($date)).rand(1000,9999);
}

//比较数字大小，如果$c部位空，$c>$b
function compareNumber($a,$b,$c=''){
    if($c == ''){ //$c为空
        if($a > $b){ //大于
            return '>';
        }elseif($a <$b){ //小于
            return '<';
        }else{ //等于
            return '=';
        }
    }else{ //$c为不为空
        if($a > $c){ //大于
            return '>';
        }elseif($a <$b){ //小于
            return '<';
        }else{ //介于
            return '=';
        }
    }
}

//通过身份证号获取生日(返回值为时间戳)

function getBirthday($id){
    return date('Y-m-d',strtotime(substr($id,6,4).substr($id,10,2).substr($id,12,2)));
}

function fetchDatetime(){
    return date('Y-m-d H:i:s');
}

function fetchDate(){
    return date('Y-m-d');
}

function strToWhere($str){
    return strpos($str,',') ? array('in',explode(',',$str)) : array('eq',$str);
}